package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.requestbean.BaseResponseBean;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.CustomerEditRequestFilter;
import com.mmlc.utils.ToastUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/11/29.
 */
public class EditCustomerActivity extends InterActivity implements View.OnClickListener {
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.iv_back)
    ImageView ivBack;
    @InjectView(R.id.rl_title)
    RelativeLayout rlTitle;
    @InjectView(R.id.edt_name)
    EditText edtName;
    @InjectView(R.id.edt_phone)
    EditText edtPhone;
    @InjectView(R.id.tv_save)
    TextView tvSave;

    @Override
    protected int initContentView() {
        return R.layout.activity_editcustomer;
    }

    public String id,name,phone;
    public int type,position;
    @Override
    protected void initView() {
        ButterKnife.inject(this);
        setEdtStyle();
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        type = intent.getIntExtra("type",0);
        name = intent.getStringExtra("name");
        phone = intent.getStringExtra("phone");
        position = intent.getIntExtra("position",0);
        edtName.setText(name);
        edtPhone.setText(phone);
    }

    @OnClick({R.id.iv_back,R.id.tv_save})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                verify();
                break;

        }
    }

    private void verify() {

        if(TextUtils.isEmpty(edtName.getText().toString())){
            ToastUtils.showToast("请输入客户姓名");
            return;
        }
        if(TextUtils.isEmpty(edtPhone.getText().toString())){
            ToastUtils.showToast("请输入客户电话");
            return;
        }
        if(name.equals(edtName.getText().toString())&&edtPhone.equals(edtPhone.getText().toString())){
            ToastUtils.showToast("未做任何更改");
            return;
        }

        CustomerEditRequestFilter filter = new CustomerEditRequestFilter((BaseParentActivity) activity);
        filter.requestBean.id = Integer.parseInt(id);
        filter.requestBean.name = edtName.getText().toString();
        filter.requestBean.phone = edtPhone.getText().toString();
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<BaseResponseBean>() {
            @Override
            public void onSuccess(BaseResponseBean result) {
                ToastUtils.showToast("修改成功");
                sucessDo();

            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess.toString());
            }
        });
    }

    private void sucessDo() {
        Intent intent = new Intent();
        intent.putExtra("type",type);
        intent.putExtra("position",position);
        setResult(200,intent);
        finish();
    }

    public void setEdtStyle() {
        // 新建一个可以添加属性的文本对象
        SpannableString ss = new SpannableString("客户姓名");
        // 新建一个属性对象,设置文字的大小
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(14, true);
        // 附加属性到文本
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置hint
        edtName.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失

        ss = new SpannableString("手机号码");
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置hint
        edtPhone.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失
    }
}
